﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Page;
using ET;
using BLL;
using HTML;

namespace ERP.PageUC
{
    public partial class QuotesUC : UserControl
    {
        #region[Define]
        protected string StrAlert = string.Empty;
        private readonly Quotes_BLL clsQuotes = new Quotes_BLL();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetQuotes();
                txtMaBaoGia.Text = MaTuDong();
                txtThoiGian.Text = DateTime.Now.ToString();
                BindDropDown();
                GetQuotes_Materials();
                GetMaterials();
                GetQuotes_Provision();
                GetQuotes_Currency();
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            OpenDialog("#baogia");
            lbID.Value = string.Empty;
            txtMaBaoGia.Enabled = true;
            Quotes_Product_BLL.Deletetb_Quotes_Product_Default(-1);
            ClearTextBox();
            GetQuotes_Materials();
            GetQuotes_Provision();
            upNewUpdatePanel.Update();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                var chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                if (chkHide.Checked)
                {
                    try
                    {
                        var dataKey = ListViewAll.DataKeys[i];
                        if (dataKey != null) Quotes_BLL.Deletetb_Quotes(int.Parse(dataKey.Value.ToString()));
                    }
                    catch
                    {
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Chứng từ này còn tồn tại !";
                        OpenDialog("#thongbao");
                        ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            GetQuotes();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert = Alert.AlertWarning("Vui lòng nhập vào từ khoá tìm kiếm !");
            }
            else
            {
                GetQuotes();
            }
        }
        protected void btnReload_Click(object sender, EventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetQuotes();
        }
        public void GetQuotes()
        {
            DataTable dtQuotes = clsQuotes.BindGirdtb_Quotes(txtSearch.Text);
            if (dtQuotes != null)
            {
                ListViewAll.DataSource = dtQuotes;
                ListViewAll.DataBind();
                if (txtSearch.Text != "" && dtQuotes.Rows.Count > 0)
                {
                    StrAlert = Alert.AlertSucess(dtQuotes.Rows.Count.ToString());
                }
            }
            else
            {
                StrAlert = Alert.AlertError("Bảng này không tồn tại");
            }
        }
        public void GetQuotesDetail(int id)
        {
            DataTable dtQuotes = clsQuotes.BindGirdtb_QuotesDetail(id);
            if (dtQuotes != null)
            {
                lbID.Value = dtQuotes.Rows[0]["Quotes_ID"].ToString();
                txtTieuDe.Text = dtQuotes.Rows[0]["Quotes_Title"].ToString();
                txtMaBaoGia.Text = dtQuotes.Rows[0]["Quotes_No"].ToString();
                drKhachHang.SelectedValue = dtQuotes.Rows[0]["Customer_ID"].ToString();
                DetailCustomer();
                txtThoiGian.Text = dtQuotes.Rows[0]["Quotes_Time"].ToString();
                txtNoiDung.Text = dtQuotes.Rows[0]["Quotes_Content"].ToString();
                txtPhiVanChuyen.Text = dtQuotes.Rows[0]["Quotes_ShippingFee"].ToString();
                drLoaiTien.SelectedValue = dtQuotes.Rows[0]["Currency_ID"].ToString();
                txtLoiChao.Text = dtQuotes.Rows[0]["Quotes_Greeting"].ToString();
            }
        }
        private void BindDropDown()
        {
            var clsKhachHang = new Customer_BLL();
            var dtKhachHang = clsKhachHang.BindGirdtb_Customer(string.Empty);
            if (dtKhachHang != null && dtKhachHang.Rows.Count > 0)
            {
                drKhachHang.Items.Clear();
                drKhachHang.Items.Add(new ListItem("-Vui lòng chọn khách hàng-", "-1"));
                drKhachHang.DataSource = dtKhachHang;
                drKhachHang.DataValueField = "Customer_Id";
                drKhachHang.DataTextField = "Customer_Alias";
                drKhachHang.DataBind();
            }
        }
        private void GetQuotes_Provision()
        {
            Quotes_Provision_BLL clsQuotes_Provision = new Quotes_Provision_BLL();
            DataTable dtQuotes_Provision = clsQuotes_Provision.BindGirdtb_Quotes_Provision(lbID.Value == string.Empty ? -1 : int.Parse(lbID.Value));
            if (dtQuotes_Provision != null)
            {
                ListViewProvision.DataSource = dtQuotes_Provision;
                ListViewProvision.DataBind();
            }
        }

        #region[Function]
        public void GetQuotes_Materials()
        {
            Quotes_Product_BLL clsQuotes_Product = new Quotes_Product_BLL();
            DataTable dtQuotes_Product = clsQuotes_Product.BindGirdtb_Quotes_Product(lbID.Value == string.Empty ? -1 : int.Parse(lbID.Value));
            if (dtQuotes_Product != null)
            {
                ListViewProductQuotes.DataSource = dtQuotes_Product;
                ListViewProductQuotes.DataBind();
            }
        }
        public void GetMaterials()
        {
            Materials_BLL clsMaterials = new Materials_BLL();
            DataTable dtMaterials = clsMaterials.Search(string.Empty);
            if (dtMaterials != null)
            {
                ListViewQuotesProduct.DataSource = dtMaterials;
                ListViewQuotesProduct.DataBind();
            }
        }
        public void GetQuotes_Currency()
        {
            Currency_BLL cls = new Currency_BLL();
            DataTable dt = cls.BindGirdtb_Currency(string.Empty);
            if (dt != null)
            {
                drLoaiTien.Items.Clear();
                drLoaiTien.Items.Add(new ListItem("-Vui lòng chọn loại tiền-", "-1"));
                drLoaiTien.DataTextField = "Currency_Name";
                drLoaiTien.DataValueField = "Currency_ID";
                drLoaiTien.DataSource = dt;
                drLoaiTien.DataBind();
            }
        }
        public void Sum()
        {
            Quotes_Product_BLL cls = new Quotes_Product_BLL();
            DataTable dt = cls.SumQuotes(lbID.Value == string.Empty ? -1 : int.Parse(lbID.Value));
            if (dt != null)
            {
                txtTongTienHang.Text = dt.Rows[0]["SumPrice"].ToString();
                txtTongTienVAT.Text = dt.Rows[0]["SumVAT"].ToString();
                float x = float.Parse(txtPhiVanChuyen.Text) + float.Parse(dt.Rows[0]["SumTotalCash"].ToString());
                txtTongCong.Text = x.ToString();
            }
        }
        #endregion[Function]

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertQuotes();
            }
            else
            {
                UpdateQuotes();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            GetQuotes_Materials();
            GetMaterials();
            GetQuotes_Provision();
            Sum();
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsQuotes = new Quotes_BLL();
            var dtQuotes = clsQuotes.BindGirdtb_Quotes(string.Empty);

            Quotes_Product_BLL clsQuotes_Product = new Quotes_Product_BLL();
            DataTable dtQuotes_Product = clsQuotes_Product.BindGirdtb_Quotes_Product(-1);

            if (dtQuotes != null && dtQuotes.Rows.Count > 0)
            {
                foreach (DataRow dr in dtQuotes.Rows)
                {
                    if (txtMaBaoGia.Text == dr["Quotes_No"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã báo giá !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    else if (dtQuotes_Product.Rows.Count == 0)
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn sản phẩm báo giá !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected bool CheckInsertMaterials(string a, string b)
        {
            bool kiemtra = false;
            Quotes_Product_BLL clsQuotes_Product = new Quotes_Product_BLL();
            DataTable dtQuotes_Product = clsQuotes_Product.BindGirdtb_Quotes_Product(lbID.Value == string.Empty ? -1 : int.Parse(lbID.Value));
            if (dtQuotes_Product != null && dtQuotes_Product.Rows.Count > 0)
            {
                foreach (DataRow dr in dtQuotes_Product.Rows)
                {
                    if (a == dr["Materials_ID"].ToString() && dr["Quotes_ID"].ToString() == b)
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Mặt hàng này đã có vui lòng cập nhật lại !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertQuotes()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Quotes_ET
                    {
                        Quotes_No = txtMaBaoGia.Text,
                        Quotes_Time = DateTime.Parse(txtThoiGian.Text),
                        Customer_ID = int.Parse(drKhachHang.SelectedValue),
                        Employee_ID = 0,
                        Quotes_UnitPrice = float.Parse(txtTongTienHang.Text),
                        Quotes_VAT = float.Parse(txtTongTienVAT.Text),
                        Quotes_ShippingFee = float.Parse(txtPhiVanChuyen.Text),
                        Quotes_TotalCash = float.Parse(txtTongCong.Text),
                        Quotes_Title = txtTieuDe.Text,
                        Quotes_Content = txtNoiDung.Text,
                        Quotes_Greeting = txtLoiChao.Text,
                        Currency_ID = int.Parse(drLoaiTien.SelectedValue),
                        Quotes_Date_Add = DateTime.Now,
                        Quotes_Date_Modify = DateTime.Now
                    };
                    Quotes_BLL.Inserttb_Quotes(obj);
                    GetQuotes();
                    ClearTextBox();
                    BindDropDown();
                    GetQuotes_Materials();
                    GetMaterials();
                    GetQuotes_Provision();
                    GetQuotes_Currency();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaBaoGia.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateQuotes()
        {
            //try
            //{
                var obj = new Quotes_ET
                {
                    Quotes_ID = int.Parse(lbID.Value),
                    Quotes_No = txtMaBaoGia.Text,
                    Quotes_Time = DateTime.Parse(txtThoiGian.Text),
                    Customer_ID = int.Parse(drKhachHang.SelectedValue),
                    Employee_ID = 0,
                    Quotes_UnitPrice = float.Parse(txtTongTienHang.Text),
                    Quotes_VAT = float.Parse(txtTongTienVAT.Text),
                    Quotes_ShippingFee = float.Parse(txtPhiVanChuyen.Text),
                    Quotes_TotalCash = float.Parse(txtTongCong.Text),
                    Quotes_Title = txtTieuDe.Text,
                    Quotes_Content = txtNoiDung.Text,
                    Quotes_Greeting = txtLoiChao.Text,
                    Currency_ID = int.Parse(drLoaiTien.SelectedValue),
                    Quotes_Date_Modify = DateTime.Now,
                    Quotes_Computer_Modify = Environment.MachineName
                };
                Quotes_BLL.Updatetb_Quotes(obj);
                GetQuotes();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            //}
            //catch (Exception ex)
            //{
            //    if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            //}
            //finally
            //{
                txtMaBaoGia.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            //}
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaBaoGia.Text = MaTuDong();
            txtTenKhachHang.Text = string.Empty;
            txtThoiGian.Text = DateTime.Now.ToString();
            txtDiaChi.Text = string.Empty;
            txtLienhe.Text = string.Empty;
            txtChucVu.Text = string.Empty;
            txtTongTienHang.Text = "0";
            txtTongTienVAT.Text = "0";
            txtPhiVanChuyen.Text = "0";
            txtTongCong.Text = "0";

            drKhachHang.SelectedIndex = 0;
            drLoaiTien.SelectedIndex = 0;
        }
        #endregion

        void DetailCustomer()
        {
            if (drKhachHang.SelectedValue == "-1")
            {
                txtTenKhachHang.Text = string.Empty;
                txtDiaChi.Text = string.Empty;
                txtLienhe.Text = string.Empty;
                txtChucVu.Text = string.Empty;
            }
            else
            {
                var clsKhachHang = new Customer_BLL();
                var dtKhachHang = clsKhachHang.GetCustomerDetail(int.Parse(drKhachHang.SelectedValue));
                if (dtKhachHang != null)
                {
                    txtTenKhachHang.Text = dtKhachHang.Rows[0]["Customer_Name"].ToString();
                    txtDiaChi.Text = dtKhachHang.Rows[0]["Customer_Address"].ToString();
                    txtLienhe.Text = dtKhachHang.Rows[0]["Contact_Name"].ToString();
                    txtChucVu.Text = dtKhachHang.Rows[0]["Job_Name"].ToString();
                }
            }
        }
        protected void drKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            DetailCustomer();
        }

        protected void btnSavePopupQuotes_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewQuotesProduct.Items.Count; i++)
            {
                var chkHide = (CheckBox)ListViewQuotesProduct.Items[i].FindControl("chkListQuotes");
                if (chkHide.Checked)
                {
                    var dataKey = ListViewQuotesProduct.DataKeys[i].Value;
                    if (dataKey != null)
                    {
                        try
                        {
                            if (!CheckInsertMaterials(dataKey.ToString(), lbID.Value == string.Empty ? "-1" : lbID.Value))
                            {
                                var obj = new Quotes_Product_ET
                                {
                                    Materials_ID = (int)(dataKey),
                                    Quotes_ID = lbID.Value == string.Empty ? -1 : int.Parse(lbID.Value)
                                };
                                Quotes_Product_BLL.Inserttb_Quotes_Product(obj);
                                GetQuotes_Materials();
                                upNewUpdatePanel.Update();
                                if (Page.Master != null)
                                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm sản phẩm thành công !";
                            }
                        }
                        catch (Exception ex)
                        {
                            if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
                        }
                        finally
                        {
                            OpenDialog("#thongbao");
                            if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                        }
                    }
                }
            }
        }

        protected void btnAdd_Quotes_Product_Click(object sender, EventArgs e)
        {
            OpenDialog("#baogia_themsanpham");
            GetMaterials();
        }

        protected void ListViewProductQuotes_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                GetQuotes_Materials();
            }
        }

        protected void ListViewProductQuotes_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            Label lbIDVatTu = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("lbID")) as Label;
            Quotes_Product_BLL.Deletetb_Quotes_Product(int.Parse(lbIDVatTu.Text));

            ListViewProductQuotes.EditIndex = -1;
            GetQuotes_Materials();
            Sum();
            if (Page.Master != null)
                ((Label)Page.Master.FindControl("lbThongBao")).Text = "Xoá vật tư ra khỏi danh sách thành công !";
            OpenDialog("#thongbao");
            ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
        }

        protected void ListViewProductQuotes_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ListViewProductQuotes.EditIndex = e.NewEditIndex;
            GetQuotes_Materials();
        }

        protected void ListViewProductQuotes_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            try
            {
                Label lbIDProductQuotes = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("lbID")) as Label;
                Label lbIDVatTu = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("lbIDVatTu")) as Label;

                TextBox txtSoLuong = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("txtSoLuong")) as TextBox;
                TextBox txtDonGia = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("txtDonGia")) as TextBox;
                Label txtThanhTien = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("txtThanhTien")) as Label;

                TextBox txtVATPercent = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("txtVATPercent")) as TextBox;
                TextBox txtVAT = (ListViewProductQuotes.Items[e.ItemIndex].FindControl("txtVAT")) as TextBox;

                txtSoLuong.Attributes.Add("onkeypress", "return clickButton(event,'" + txtSoLuong.ClientID + "')");

                ET.Quotes_Product_ET obj = new ET.Quotes_Product_ET();
                obj.Materials_ID = int.Parse(lbIDVatTu.Text);
                obj.Quotes_Materials_ID = int.Parse(lbIDProductQuotes.Text);
                obj.Quotes_Product_Quantity = int.Parse(txtSoLuong.Text);
                obj.Quotes_Product_Price = float.Parse(txtDonGia.Text.Replace(",", ""));

                obj.Quotes_Product_VAT_Percent = float.Parse(txtVATPercent.Text.Replace(",", ""));
                if (txtVATPercent.Text == "0" || txtVATPercent.Text == string.Empty)
                {
                    obj.Quotes_Product_VAT = 0;
                }
                else
                {
                    obj.Quotes_Product_VAT = (float.Parse(txtSoLuong.Text.Replace(",", "")) * (float.Parse(txtDonGia.Text.Replace(",", "")) * float.Parse(txtVATPercent.Text.Replace(",", "")))) / 100;
                }
                obj.Quotes_Product_TotalCash = (float.Parse(txtSoLuong.Text) * float.Parse(txtDonGia.Text.Replace(",", ""))) + (float.Parse(txtSoLuong.Text.Replace(",", "")) * (float.Parse(txtDonGia.Text.Replace(",", "")) * float.Parse(txtVATPercent.Text.Replace(",", "")))) / 100;

                obj.Quotes_ID = lbID.Value == string.Empty ? -1 : int.Parse(lbID.Value);

                Quotes_Product_BLL.Updatetb_Quotes_Product(obj);

                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Cập nhật thành công danh sách vật tư !";
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                ListViewProductQuotes.EditIndex = -1;
                GetQuotes_Materials();
                Sum();
            }
            catch (Exception ex)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        protected void ListViewProductQuotes_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            ListViewProductQuotes.EditIndex = -1;
            GetQuotes_Materials();
        }

        protected void ListViewProvision_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            ListViewProvision.EditIndex = -1;
            GetQuotes_Provision();
        }

        protected void ListViewProvision_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

        }

        protected void ListViewProvision_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            Label lbID = (ListViewProvision.Items[e.ItemIndex].FindControl("lbID")) as Label;
            Quotes_Provision_BLL.Deletetb_Quotes_Provision(int.Parse(lbID.Text));

            ListViewProvision.EditIndex = -1;
            GetQuotes_Provision();
            if (Page.Master != null)
                ((Label)Page.Master.FindControl("lbThongBao")).Text = "Xoá điều khoản thành công !";
            OpenDialog("#thongbao");
            ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
        }

        protected void ListViewProvision_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ListViewProvision.EditIndex = e.NewEditIndex;
            GetQuotes_Provision();
        }

        protected void ListViewProvision_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            try
            {
                Label lbID = (ListViewProvision.Items[e.ItemIndex].FindControl("lbID")) as Label;
                TextBox txtThemDieuKhoan = (TextBox)ListViewProvision.Items[e.ItemIndex].FindControl("txtDieuKhoan");
                TextBox txtThemNoiDungDieuKhoan = (TextBox)ListViewProvision.Items[e.ItemIndex].FindControl("txtNoiDungDieuKhoan");

                ET.Quotes_Provision_ET obj = new ET.Quotes_Provision_ET();
                obj.Quotes_Provision_ID = int.Parse(lbID.Text);
                obj.Quotes_Provision_Name = txtThemDieuKhoan.Text;
                obj.Quotes_Provision_Value = txtThemNoiDungDieuKhoan.Text;
                obj.Quotes_ID = -1;

                Quotes_Provision_BLL.Updatetb_Quotes_Provision(obj);

                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Cập nhật thành công điều khoản !";
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                ListViewProvision.EditIndex = -1;
                GetQuotes_Provision();
            }
            catch (Exception ex)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        protected void ListViewProvision_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            TextBox txtThemDieuKhoan = (TextBox)e.Item.FindControl("txtThemDieuKhoan");
            TextBox txtThemNoiDungDieuKhoan = (TextBox)e.Item.FindControl("txtThemNoiDungDieuKhoan");

            ET.Quotes_Provision_ET obj = new ET.Quotes_Provision_ET();
            obj.Quotes_Provision_Name = txtThemDieuKhoan.Text;
            obj.Quotes_Provision_Value = txtThemNoiDungDieuKhoan.Text;
            obj.Quotes_ID = lbID.Value == string.Empty ? -1 : int.Parse(lbID.Value);

            Quotes_Provision_BLL.Inserttb_Quotes_Provision(obj);

            GetQuotes_Provision();
        }

        protected void txtPhiVanChuyen_TextChanged(object sender, EventArgs e)
        {
            Sum();
        }

        #region[MaTangTuDong]
        protected string MaTuDong()
        {
            int i = 0;
            DataTable dt = Quotes_BLL.cls.BindGirdtb_Quotes(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = Convert.ToInt32(dt.Rows[0]["Quotes_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Quotes_ID"].ToString()) > max)
                    {
                        max = Convert.ToInt32(dt.Rows[i]["Quotes_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "BG." + DateTime.Now.ToString("MMdd") + "-000" + i;
        }
        #endregion

        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "_ViewDetail")
            {
                Label lbIDQuotesDetail = (Label)e.Item.FindControl("lbIDDetail");
                GetQuotesDetail(int.Parse(lbIDQuotesDetail.Text));
                GetQuotes_Materials();
                GetMaterials();
                GetQuotes_Provision();
                Sum();
                OpenDialog("#baogia");
                txtMaBaoGia.Enabled = false;
                upNewUpdatePanel.Update();
            }
        }
    }
}